﻿using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web.UI;

public partial class IFRS_WIN20_2E : PageBase
{
    #region "---------- Events -----------"

    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.AddClientScript();
            this.ViewMenu();
            this.BindDDL();
            this.SetErrorMessage();
            this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }

    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN20_1S.aspx");
    }
    #endregion "---------- Events -----------"

    #region "---------- Methods -----------"

    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";
    }

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN20_2E;
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqDate_Transection.SetRequiredField(lblDate_Transection, txtDate_Transection, btnSAVE);
        reqSourceData.SetRequiredField(lblSourceData, ddlSourceData, btnSAVE);
        reqAccountNo_ContractNumber.SetRequiredField(lblAccountNo_ContractNumber, txtAccountNo_ContractNumber, btnSAVE);    
    }

    private void BindData(int ID)
    {
        List<FairValueOption> FairValueOption = new List<FairValueOption>();
        DataTable dt = new SPR_FAIRVALUEOPTION().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, FairValueOption);
            txtDate_Transection.Text = FairValueOption[0].TransactionDate.DateToString();
            ddlSourceData.SelectedValue = FairValueOption[0].SourceDataID.ToString();
            txtAccountNo_ContractNumber.Text = FairValueOption[0].AccountNo;         
        }
    }

    private void SaveData()
    {
        try
        {
            FairValueOption fairValueOption = new FairValueOption();
            fairValueOption.FairValueOptionID = Convert.ToInt32(Request.QueryString["id"].ToString());
            fairValueOption.TransactionDate = txtDate_Transection.Text.StringToDateAllowNull();
            fairValueOption.SourceDataID = ddlSourceData.SelectedValue.AsInt32();
            fairValueOption.AccountNo = txtAccountNo_ContractNumber.Text;
            fairValueOption.SourceOfData = "WebInput";
            fairValueOption.SetStandardField();           

            int result = new SPR_FAIRVALUEOPTION().UPDATE(fairValueOption);

            if (result > 0)
            {
                this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN20_2E.aspx?&id={0}", fairValueOption.FairValueOptionID)));
            }
            else
            {
                if (result == -1)
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                }
                else
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void BindDDL()
    {
        ddlSourceData.SetData(DDL_TYPE.SourceData, "", false, true);
    }

    #endregion ""---------- Methods -----------"
}